--
-- Author:      name
-- DateTime:    2018-05-28 15:57:48
-- Description: 游戏主逻辑

local skynet = require "skynet"
local log = require "Log"
local config = require "configquery"
local Timer = require "timer"
local FSM = require "fsm"
local NodeMessage = require "NodeMessage"
local Universal = require "Universal"
local json = require "cjson"
require "common"


local mix_array = mix_array
local table_clone = table_clone
local floor = math.floor
local four_five = math.four_five
local random = math.random

-- local player_list 
-- local seat_list 

local testtableid = 10020

local Machine = class("Machine")

function Machine:ctor(tableid)
	self.close = false --关闭游戏
	self.nodeMessage = NodeMessage.new()
	--共用类
	self.universal = Universal.new()

	self.player_list = {} --玩家列表
	self.seat_list = {} --座位列表
	self.tableid = tableid --桌子id

	self.gameid = getGameIdbyTable(tableid)
	self.roomid = getRoomType(tableid)
	local cfgname = getGameRoomConfig(self.gameid)
	local roomcfg = config[cfgname][self.roomid]

	--配置信息
	self.maxSeatNum = roomcfg.player_num --最大座位数
	self.bottomNum = roomcfg.bottom_score * 0.01 --底分
	self.tipRate = roomcfg.tip * 0.01 --服务费比
	self.incomeRate = 1 - self.tipRate --收入占比
	self.minChip = roomcfg.min_coin --入场最低分
	self.topChip = roomcfg.max_coin --入场最高分
	self.maxOperTimeoutNum = 3 --最多操作超时次数

	--状态时间
	self.status_time = {
		wait = 1,		--等待时间
		rest = 5,		--休息时间
		start = 4,		--开始
		oper = 10,		--下注等操作时间
		compare = 5,	--选择比牌操作时间
		open = 3,		--比牌
		settlement = 4, --结算时间
	}
	self.operList = {
		--1跟注，2加注，3看牌，4比牌，5押满，6弃牌，7下底注
		follow =1,
		add = 2,
		look = 3,
		compare = 4,
		allin = 5,
		giveup = 6,
		buttom_bet = 7,
	}
	self.playerState = {
		--已看牌，已放弃，已输牌，
		playing = 1,
		look = 2,
		giveup = 3,
		lose = 4,
		win = 5,
		loseEnd = 6,
		loseAllin = 7,
	}
	--加注列表
	self.addChipList = {
		[0.01]= {'0.02','0.05','0.1'},
		[1]= {'2','5','10'},
		[5]= {'10','20','50'},
		[10]= {'20','100','200'},
	}
	--配置信息


	self.maxRound = 8 --最大轮数
	self.curRound = 1 --当前轮
	self.playerNum = 0 --参与游戏人数
	self.allinPlayer = nil --第一个全押的人

	self.handlerStateTime = nil --定时器句柄
	self.tbBeforeStart = {} --开始游戏前所捅有筹码

	self.allBetChip = 0 --共下注
	self.followChip = 0 --当前跟注值

	self.operStartTime = 0 --当前状态的开始时间
	self.chipList = {} --玩家已下注数值列表
	
	self.gmcards = {} --配牌列表
	self.tbCheatNum = {} --配牌标记
	self.paijuid = 0 --牌局ID
	self.tbSeatCard = {} --座位上的牌
	self.tbSeatRes = {} --座位结果
	self.winnerSeat = nil --赢家座位
	self.closeHandler = nil

	self.rule = g_objects:getOne("Rule")
	self.operSeatid = nil  --当前操作的玩家id
	self.startSeatid = 1 --开始操作的座位id
	self.nextOperSeat = true
	-- self.curOperTimeHandler = nil --当前操作的玩家定时器句柄
	self.compareTimehandler = nil --比牌选择时间 
	self.stock = {} --库存
	self:getStock()
	print("self.stock_____",self.stock)

	--状态
	--等侍，休息，开始， 玩家操作(8轮)，比牌，结算
	--state1 --eventName --state2 --callback
	self.gameStateTransitionTable = {
		{"rest", 		"to_start", 			"start", 		function() self:onStart() end },
		{"start", 		"to_oper",				"oper", 		function() self:onOper() end },
		{"oper", 		"to_compare",			"compare", 		function() self:onCompare() end },
		{"oper", 		"oper_to_open",			"open", 		function() self:onOpen() end },		
		{"compare", 	"to_open",				"open", 		function() self:onOpen() end },
		{"open", 		"open_to_oper",			"oper", 		function() self:onOper() end },
		{"oper", 		"oper_to_settlement",	"settlement", 	function() self:onSettlement() end },
		{"open", 		"open_to_settlement",	"settlement", 	function() self:onSettlement() end },
		{"settlement", 	"to_rest", 				"rest", 		function() self:onRest() end},
		{"rest", 		"to_wait", 				"wait", 		function() self:onWait() end},		
		{"wait", 		"to_rest", 				"rest", 		function() self:onRest() end},	
	}
	--计时器
	self.timer = Timer:new()
	--状态机
	self.fsm = FSM.new(self.gameStateTransitionTable)
	
end
---------------------------------------------------------
-- private
---------------------------------------------------------
--重置数据
function Machine:reset()

	self.tbSeatCard = {} 
	self.tbSeatRes = {}
	self.playerNum = 0
	self.chipList = {}
	self.allinPlayer = nil
	self.tbBeforeStart = {}
	--玩家自身状态
	for k,v in pairs(self.player_list) do 
		v.playing = false
		v.giveup = false
		v.look = false
		v.lose = false
		v.win = false
		v.loseEnd = false --最后开牌时输了
		v.loseAllin = false --全押输了
		v.betChip = 0     --已下的金币值
		v.bet = 0         --上次所下金币值
		v.oper = nil      --上次所做的操作
		v.isRecorded = false --已写到牌局记录	
	end

	self.allBetChip = 0
	self.followChip = 0
	self.nextOperSeat = true
	self.curRound = 1
	self.winnerSeat = nil
	self.operStartTime = 0
	-- self.curOperTimeHandler = nil --当前操作的玩家定时器句柄
	self.compareTimehandler = nil --比牌选择时间 
	self.operSeatid = nil  --当前操作的玩家id
end

--状态定时器
function Machine:setTime(time,callback,loop,param)
	if self.handlerStateTime then 
		self.timer:unregister(self.handlerStateTime)
		self.handlerStateTime = nil	
	end
	if not time then 
		return 
	end
	self.handlerStateTime = self.timer:register(time , param, callback,loop)	
end

--更新大厅中传来的玩家数据
function Machine:updateProperty(playerid,data)
	local player = self.player_list[playerid]
	if not player then 
		return 
	end
	for k,v in pairs(data) do 
		player[k] = v
	end
	local seatPlayer = self:getSeatPlayer(player)
	local data = {zjh_nt_seat_change = {
		tableid = self.tableid,
		seatid = seatid,
		seat_player = seatPlayer,
		leave = false,
	}}
	self:sendToAll(data)	
end

--更新人数
function Machine:updatePlayerNum( )
	local c = self:getTablePlayerNum()
	self.nodeMessage:sendGlobalService('tablestate', 'set', self.tableid, c)
end

--桌子人数
function Machine:getTablePlayerNum()
	local c = 0
	for k,v in pairs(self.player_list) do
		c = c + 1
	end
	return c
end

--离线清除
function Machine:removePlayer()
	for k,v in pairs(self.player_list) do 
		local timeoutGiveupNum = v.timeoutGiveupNum
		if v.disconnect or not v.ready or v.chip < self.minChip or (timeoutGiveupNum and timeoutGiveupNum >= self.maxOperTimeoutNum) then 	
			if  v.chip < self.minChip then 
				local data = {nt_kick_table = {
					playerid = k,
					content = '金币不足请离场！',
				}}
				self:sendMessage(k,data)
			end			

			if timeoutGiveupNum and timeoutGiveupNum >= self.maxOperTimeoutNum then
				local data = {nt_kick_table = {
					playerid = k,
					content = '您已长时间未操作，将自动退出游戏。',
				}}
				self:sendMessage(k,data)
			end	
			--设置玩家所在桌子id为0	
			self:sendGate(v, 'update_tableid', 0)
			self:sitUp(k)
			self.player_list[k] = nil
			print("炸金花踢走掉线 or 没准备好 or 筹码不够 or 太久没有操作#######",k)
		end
	end
end

--离线清除
function Machine:removeDisconnect()
	for k,v in pairs(self.player_list) do 
		if v.disconnect then 	
			self:sitUp(k)
			self.player_list[k] = nil
			--清除玩家所在桌子id
			self:sendGate(v, 'update_tableid', 0)	
			print("踢走离线#######",k)		
		end
	end
end

--玩家状态值 
function Machine:getPlayerState(player)
	local state = 0 
	if player.playing then
		state = self.playerState.playing
	end	
	if player.look then
		state = self.playerState.look
	end
	if player.giveup then
		state = self.playerState.giveup
	end	
	if player.lose then
		state = self.playerState.lose
	elseif player.win then 
		state = self.playerState.win
	elseif player.loseEnd then 
		state = self.playerState.loseEnd
	elseif player.loseAllin then 
		state = self.playerState.loseAllin
	end			
	return state
end

--玩家结束这局游戏时的状态
function Machine:getEndState(player)
	if player.lose then 
		return "比牌输"
	elseif player.giveup then 
		return "弃牌"
	elseif player.win then 
		return "赢"
	elseif player.loseEnd then 
		return "开牌输"
	elseif player.loseAllin then 
		return "全押输"
	end
end

--取seatplayer
function Machine:getSeatPlayer(player)
	local seatPlayer = {}
	seatPlayer.seatid = player.seatid
	seatPlayer.playerid = player.playerid
	seatPlayer.nick = player.nick
	seatPlayer.chip = ""..player.chip
	seatPlayer.silver = player.silver
	seatPlayer.face = player.face
	seatPlayer.icon = player.icon
	seatPlayer.viplevel = player.viplevel
	seatPlayer.level = player.level

	seatPlayer.playing = player.playing
	seatPlayer.bet_chip = tostring(player.betChip or 0)
	seatPlayer.state = self:getPlayerState(player)

	return seatPlayer
end

--取当前正在局里玩家数
function Machine:getPlayingNum()
	local num = 0
	for k, v in pairs(self.player_list) do 
		if self:isPlaying(v) then 
			num = num + 1
		end
	end	
	return num 
end

--是否正在局中玩
function Machine:isPlaying(player)
	if player and player.playing and not player.lose and not player.giveup then 
		return true
	end
	return false
end

--通过座位取玩家
function Machine:getPlayerBySeat(seatid)
	local playerid = self.seat_list[seatid]
	if not playerid then return end
	return self.player_list[playerid]
end

--设置当前操作的座位
function Machine:nextOperSeatid()
	local player
	local operSeatid = self.operSeatid
	self.nextOperSeat = false
	if not operSeatid then --从上局赢家开始操作
		operSeatid = self.startSeatid
		player = self:getPlayerBySeat(operSeatid)
		if self:isPlaying(player) then 
			print("_111_______________ 当前轮数_____",self.curRound)
			self.operSeatid = operSeatid
			self:ntOper(operSeatid)
			return operSeatid			
		end
	end
	while(true) do 
		operSeatid = operSeatid + 1
		if operSeatid > self.maxSeatNum then 
			operSeatid = 1
		end		
		if operSeatid == self.startSeatid then --1轮结束						
			if self.curRound == self.maxRound then --到达最大轮数开牌	
				self:ntOper(nil, self.operSeatid)			
				local callback = function ( ... )
					self.fsm:fire("oper_to_settlement")
				end
				self:setTime(1, callback)
				return
			end
			self.curRound = self.curRound + 1
			print("_222_______________ 当前轮数_____",self.curRound)
		end		
		player = self:getPlayerBySeat(operSeatid)
		if self:isPlaying(player) then 
			break
		end
	end
	local oldOperSeat = self.operSeatid
	self.operSeatid = operSeatid
	self:ntOper(operSeatid,oldOperSeat)

	return operSeatid
end

--玩家可作操作
function Machine:getPlayerOperList(player)
	local tbOper = {self.operList.giveup}
	local rate = 1 
	if player.look then 
		rate = 2
	end
	--第2轮后才能看牌
	if self.curRound > 1 and not player.look then 
		table.insert(tbOper,self.operList.look)
	end
	--所持有金币大于当前跟注值
	if self.followChip*rate <= player.chip then 
		table.insert(tbOper,self.operList.follow)
	else
		--不够跟注只能比牌
		table.insert(tbOper,self.operList.compare)
		return tbOper
	end
	--已押满
	if self.followChip == self.bottomNum * 100 then 
		table.insert(tbOper,self.operList.allin)
		return tbOper
	end	

	--是否能加注
	local tbAdd = self.addChipList[self.bottomNum]
	--print(tbAdd)
	local addNum
	for k,v in pairs(tbAdd) do 
		if self.followChip < tonumber(v) then 
			addNum = tonumber(v)*rate
			break
		end
	end
	if addNum and addNum <= player.chip then 
		table.insert(tbOper,self.operList.add)
	end

	--第3轮后能才比牌
	if self.curRound > 2 and player.chip >= self.followChip * 2 then 
		table.insert(tbOper,self.operList.compare)
	end

	--只剩下2人时可押满; 最后一轮第一个下注人
	if self.curRound > 3 and self:getPlayingNum() == 2 and self.curRound < self.maxRound or
	    self:getPlayingNum() == 2 and self.curRound == self.maxRound and self.startSeatid == player.seatid then 
		table.insert(tbOper,self.operList.allin)
	end

	return tbOper
end

--玩家加注值列表
function Machine:getPlayerAddList(player)
	local tb 
	local tbAdd = self.addChipList[self.bottomNum]
	local tbNum = {}
	local rate = 1
	if player.look then 
		rate = 2
	end
	tb = {tostring(self.followChip)*rate}
	for k,v in pairs(tbAdd) do 
		table.insert(tb,tostring(tonumber(v)*rate))
	end

	return tb
end

---------------------------------------------------------
-- 发送消息
---------------------------------------------------------
--需要返回的请求
function Machine:callGate(player,cmd,data)
	return self.nodeMessage:callGate(player.nodename,player.playerid,cmd,data)
end

function Machine:sendGate(player,cmd,data)
	self.nodeMessage:sendGate(player.nodename,player.playerid,cmd,data)
end

function Machine:sendMessage(playerid,data)
	local player = self.player_list[playerid]
	if not player or player.disconnect or not player.ready then 
		return
	end	
	self.nodeMessage:broadcastGate(player.nodename,{playerid},data)
end

function Machine:sendToAll(data)
	local playerList = {}	
	for k,v in pairs(self.player_list) do 
		if not v.disconnect and v.ready then 
			table.insert(playerList,k)
		end
	end
	self.nodeMessage:broadcastAllGate(playerList, data)
end

function Machine:sendToOther(playerid,data)
	local playerList = {}
	for k,v in pairs(self.player_list) do 
		if (not v.disconnect and k ~= playerid) and  v.ready then 	
			table.insert(playerList,k)
		end
	end
	self.nodeMessage:broadcastAllGate(playerList, data)
end
function Machine:reconnect(playerid)
	-- return self.tbMessage[playerid]
end


---------------------------------------------------------
-- 状态
---------------------------------------------------------
--等侍
function Machine:onWait()
	print("_______wait")
	local timehandler = nil
	local callback = function()
		if timehandler then
			return 
		end	
		self:removeDisconnect()
		--人数足2个才开始
		local num = 0
		for k,v in pairs(self.player_list) do 
			num = num + 1
		end
		if num < 2 or timehandler then 
			return
		end		
		--
		local callback2 = function ( )		
			self.fsm:fire("to_rest")			
		end
		timehandler = self.timer:register(self.status_time.wait,nil,callback2)
				
	end
	self:setTime(self.status_time.wait,callback,true)
end

--休息
function Machine:onRest()
	print("_______rest")
	self.operStartTime = os.time()
	--洗牌
	self.rule:shuffle()
	--重置数据
	self:reset()
	self:ntRest()
	self:removePlayer()
	local function  checkPlayer()
		--人数足2个才开始
		local num = 0
		for k,v in pairs(self.player_list) do 
			if not v.disconnect and v.ready then 
				num = num + 1
			end
		end
		if num < 2 then 
			self.fsm:fire("to_wait")
			return false
		end	
		if self.close then 
			return false
		end
		return true
	end
	if not checkPlayer() then 
		return 
	end
	local callback = function ( ... )
		if not checkPlayer() then 
			return 
		end
		self.fsm:fire("to_start")
	end
	self:setTime(self.status_time.rest,callback)

end

--开始
function Machine:onStart()
	print("_______start")	

	self.paijuid = getRoundId(self.tableid)
	self.operStartTime = os.time()	
	local num = 0
	local allBottomChip = 0	
	local player
	local tbPlayerSeat = {}
	local money
	local reason = self.universal:getReasonId('zjh_xz')
	--准备好的玩家
	for k,v in pairs(self.seat_list) do 
		player = self.player_list[v]
		if player.ready then 
			player.playing = true
			num = num + 1
			allBottomChip = allBottomChip + self.bottomNum
			if not player.betChip then 
				player.betChip = 0
			end
			player.betChip = player.betChip + self.bottomNum
			player.oper = self.operList.buttom_bet
			player.bet = self.bottomNum

			table.insert(self.chipList,tostring(self.bottomNum))
			table.insert(tbPlayerSeat,k)		
			--发牌3张
			local card = self:getCard(v,3) 
			print("__________card__",card)
			self.tbSeatCard[k] = card
			self.tbSeatRes[k] = self.rule:getCardWeight(card)
			self.tbBeforeStart[v] = player.chip
			money = self:callGate(player, 'change_money', -player.bet, reason)
			if money then 
				player.chip = money
			end
		end
	end
	self.allBetChip = allBottomChip
	self.playerNum = num
	self.followChip = self.bottomNum
	
	--每个人下底注
	self:ntStart()
	self:ntCardToRoot()
	local callback = function()
		self.fsm:fire("to_oper")
	end
	self:setTime(self.status_time.start,callback)
end

--玩家操作
function Machine:onOper()
	--print("_______bet")	
	--local time  = 0
	local callback = function()	
		if not self.nextOperSeat then 
			return
		end
		local seatid = self:nextOperSeatid()
		if not seatid then 
			return
		end
		if self:getState() ~= 'oper' then 
			return 
		end
		self.operStartTime = os.time()
		local playerOperFun = function ()
			if self:getState() ~= 'oper' then 
				return 
			end
			--时间到玩家没操作视为弃牌
			self:giveup(self.operSeatid)
			local player = self:getPlayerBySeat(self.operSeatid)
			player.timeoutGiveupNum = (player.timeoutGiveupNum or 0) + 1
		end
		--self.curOperTimeHandler = self.timer:register(self.status_time.oper,nil,playerOperFun)
		self:setTime(self.status_time.oper,playerOperFun)		
	end
	--self:setTime(1,callback,true)	
	skynet.fork(function()
		while(true) do 
			if self:getState() ~= 'oper' then 
				return 
			end
			callback()
			skynet.sleep(30)
		end
	end)
end

--选择比牌
function Machine:onCompare()
	local tbSeat = {}
	local player = self:getPlayerBySeat(self.operSeatid)
	local playerid = player.playerid
	for k,v in pairs(self.player_list) do 
		if self:isPlaying(v) and playerid ~= k then 
			table.insert(tbSeat,v.seatid)
		end
	end
	if #tbSeat > 1 then 
		-- if lessChip then 
		-- 	self:setPlayerCompare(playerid,tbSeat[1])
		-- else
			--compare秒后自动选择
			local function callback()
				if self:getState() ~= 'compare' then 
					return
				end
				local seatid
				for k, v in pairs(self.player_list) do 
					if self:isPlaying(v) and playerid ~= k then 
						seatid = v.seatid
						break
					end
				end
				self:setPlayerCompare(playerid,seatid,true)
			end
			self.compareTimehandler = self.timer:register(self.status_time.compare,nil,callback)
		-- end
	elseif #tbSeat == 1 then--只剩下2人
		local bwin = self:setPlayerCompare(playerid,tbSeat[1],true)
		if bwin then 
			self.winnerSeat = {player.seatid}
		else
			self.winnerSeat = {tbSeat[1]}
		end
	end
	print("______________比牌_请求_____",playerid,tbSeat)
end

--比牌
function Machine:onOpen()
	print("_______open")	
	self.operStartTime = os.time()
	self.nextOperSeat = false

	local callback = function()
		--比牌后只剩下一个人
		local playingNum = self:getPlayingNum()
		-- print("________playingNum_____",playingNum)
		if playingNum == 1 then 		
			self.fsm:fire("open_to_settlement")
		else
			self.fsm:fire("open_to_oper")
			self.nextOperSeat = true		
		end
	end
	self:setTime(self.status_time.open , callback)	
end

--结算
function Machine:onSettlement()
	print("_______settlement")
	self.operStartTime = os.time()	
	local maxWeight = 0
	local winnerSeat --可能存在多个赢家
	local player 
	local cardRes 
	local maxRes

	local callback = function()
		self.fsm:fire("to_rest")
	end		
	if not self.winnerSeat then 
		--多个人坚持到最后时比大小
		--有235 与 豹子的时候要特殊处理 还没做
		self:checkStock()
		for k,v in pairs(self.player_list) do 
			if v and v.playing then 		
				cardRes = self.tbSeatRes[v.seatid]--self.rule:getCardWeight(card)							
				if not v.giveup and not v.lose and not v.loseEnd then 	
					--找最大牌的玩家			
					if not maxRes then 
						maxRes = cardRes
						winnerSeat = {}
						player = v						
						table.insert(winnerSeat,v.seatid)						
					else
						local res,same = self.rule:compareCard(maxRes,cardRes)
						if res then
							v.loseEnd = true							
						elseif same then --一样大
							table.insert(winnerSeat,v.seatid)							
						else
							player.loseEnd = true
							player = v
							maxRes = cardRes
							winnerSeat = {}
							table.insert(winnerSeat,v.seatid)
						end
					end
				end
			end
		end
		self.winnerSeat = winnerSeat
		self:setTime(self.status_time.settlement + 2, callback)
	else
		self:setTime(self.status_time.settlement, callback)
	end
	self:ntEnd()
	--更新库存
	self:setStock()
end

---------------------------------------------------------
-- public
---------------------------------------------------------
--取桌子id
function Machine:getTableId()
	return self.tableid
end

--取玩家
function Machine:getPlayer(playerid)
	if not playerid then return nil end
	return self.player_list[playerid]
end

--加入玩家
function Machine:addPlayer(player)
	self.player_list[player.playerid] = player
end

--取玩家座位id
function Machine:getPlayerSeatId(playerid)
	for k, v in pairs(self.seat_list) do 
		if v == playerid then
			return k
		end
	end
	return nil
end

--取当前操作的玩家id
function Machine:getOperSeatid()
	-- body
	return self.operSeatid
end

--坐下
function Machine:sitDown( playerid )
	local tbSeat = {}
	for id=1,self.maxSeatNum do 
		local seat_userid = self.seat_list[id]
		if not seat_userid or seat_userid == 0 then 
			table.insert(tbSeat,id)
		end
	end
	mix_array(tbSeat) --随机位置
	local seatid
	if #tbSeat > 0 then 
		seatid = tbSeat[1]
	else
		self.player_list[playerid] = nil
		return false
	end
	local player = self.player_list[playerid]
	if player then
		self.seat_list[seatid] = playerid
		player.seatid = seatid
	end
	--print(player)
	local seatPlayer = self:getSeatPlayer(player)
	local data = {zjh_nt_seat_change = {
		tableid = self.tableid,
		seatid = seatid,
		seat_player = seatPlayer,
		playerid = playerid,
		leave = false,
	}}
	self:sendToOther(playerid,data)
	self:updatePlayerNum()
	return true
end

--离座
function Machine:sitUp(playerid)
	local seatid = 0
	if not playerid or playerid <=0 then
		return
	end
	
	local player = self.player_list[playerid]
	if not player then
		return
	end
	if player.playing then 
		--牌局记录
		local seatPlayer = self:getSeatPlayer(player)
		local cardRes = self.tbSeatRes[player.seatid]		
		seatPlayer.card_data = cardRes.card
		seatPlayer.card_type = self.rule:getCardType(cardRes.weight)		
		self:paijuRecord(seatPlayer)
	end
	seatid = player.seatid
	self.seat_list[seatid] = nil
	self.player_list[playerid] = nil

	local data = {zjh_nt_seat_change = {
		tableid = self.tableid,
		seatid = seatid,
		leave = true,
	}}
	self:sendToAll(data)
	self:updatePlayerNum()
end

--取桌子状态
function Machine:getTableStatus(playerid)

	--local seatid = self:getPlayerSeatId(playerid)
	local tbCards = {}
	local seat_players = {}
	local seatPlayer
	local player
	for seatid,id in pairs(self.seat_list) do
		if id>0 then
			player =self.player_list[id]
			seatPlayer = self:getSeatPlayer(player)
			seatPlayer.oper_time = 0
			if seatid == self.operSeatid then --正在操作
				local operTime = self.status_time.oper + os.time() - self.operStartTime
				if operTime > 0 then
					seatPlayer.oper_time = operTime
				end
				seatPlayer.button_list = self:getPlayerOperList(player)				
				seatPlayer.button_bet = self:getPlayerAddList(player)
			end
			if seatPlayer.playing then --正在玩有牌
				if self.tbSeatCard[seatid] then 
					seatPlayer.card_data = {0,0,0}
				end
				if playerid == player.playerid and player.look then --看过牌了			
				    local cardRes = self.tbSeatRes[seatid]
					seatPlayer.card_data  = cardRes.card
		        end
			end
			table.insert(seat_players,seatPlayer)
		end
	end	
	local chipList = self.chipList
	if self:getState() == 'settlement' then 
		chipList = {}
	end
	local status = {
		config = self:getConfig(),
		tableid = self.tableid,
		seat_player = seat_players,
		all_chip = tostring(self.allBetChip),
		chip_list = chipList,
		follow_bet = self.followChip,
		oper_round = self.curRound,
	}	

	print("Table status################",status)	
	return status
end

--玩家操作
function Machine:playerAddChip(playerid,args)
	local operType = args.oper_type --1跟注，2加注 3押满
	local addChip = tonumber(args.add_chip)
	local player = self.player_list[playerid]
	player.timeoutGiveupNum = 0
	local oper
	local rate = 1 
	local allin
	local allinNum 
	if player.look then 
		rate = 2
	end
	if player.seatid ~= self.operSeatid then
		return false
	end
	if operType > 3 or operType < 1 then return false end
	allinNum =  self.bottomNum * 100

	if operType == 1 then 
		--跟注
		addChip = self.followChip*rate
		oper = self.operList.follow
		--别人已押满
		if self.allinPlayer then 
			oper = self.operList.allin
			addChip = allinNum
		end			
	elseif operType == 2 then 
		--加注的金额要大于当前跟注
		if addChip < self.followChip*rate then 
			return false
		end
		local tbAdd = self.addChipList[self.bottomNum]
		local canAdd = false
		for _,v in pairs(tbAdd) do 
			if tonumber(v)*rate == addChip then 
				canAdd = true
			end
		end
		if not canAdd then
			--加注值不在列表内无效
			print("##############加注值不在列表内无效________",playerid,args)
			return false
		end
		oper = self.operList.add
		
	elseif operType == 3 then 
		--押满
		if self:getPlayingNum() ~= 2 then 
			return false
		end
		addChip = allinNum
		oper = self.operList.allin
		if not self.allinPlayer then 
			self.allinPlayer = player
		end		
	end
	--金币不足
	if player.chip < addChip then 
		return false
	end
	local reason = self.universal:getReasonId('zjh_xz')
	local money = self:callGate(player, 'change_money', -addChip,reason)				
	--print("_res,money________",res,money)
	if not money then 
		--扣钱失败
		print("__________playerAddChip___扣钱失败____",playerid,addChip,player.chip)
		return false
	end	

	player.chip = money	
	--别人已押满
	if self.allinPlayer then
		self.followChip = addChip
	else
		self.followChip = addChip/rate
	end
	table.insert(self.chipList,tostring(addChip))
	self.allBetChip = self.allBetChip + addChip
	player.betChip = player.betChip + addChip
	player.bet = addChip
	player.oper = oper

	-- self.timer:unregister(self.curOperTimeHandler)
	-- self.curOperTimeHandler = nil	
	--取消操作定时器
	self:setTime()

	--如果押满了，这轮开牌
	if self.allinPlayer and self.allinPlayer.playerid ~= playerid then 
		local seatid = self.allinPlayer.seatid			
		self:ntOper(nil, player.seatid) 
		self.nextOperSeat = false
		local function callback()
			local player = self.player_list[playerid]
			local res1 = self.tbSeatRes[player.seatid]
			local res2 = self.tbSeatRes[seatid]
			if self.rule:compareCard(res1,res2) then
				self.winnerSeat = {player.seatid}
				local loser = self:getPlayerBySeat(seatid)
				loser.loseAllin = true		
			else
				player.loseAllin = true	
				self.winnerSeat = {seatid}	
			end					
			--结算
			self.fsm:fire("oper_to_settlement")
		end
		self.timer:register(2,nil,callback)
		return true
	end	
	--让下一个玩家操作
	self.nextOperSeat = true

	return true
end

--看牌
function Machine:playerLook(player)
	if self.curRound < 2 or player.look then 
		return false
	end
	player.timeoutGiveupNum = 0
	player.look = true
	-- player.oper = self.operList.look
	-- player.bet = 0
	player.state = self.playerState.look
	self:ntStateOper(player,self.operList.look)
	local seatid = player.seatid
	return {card_data=self.tbSeatRes[seatid].card,card_type = self.tbSeatRes[seatid].cardType}
end

--比牌
function Machine:playerCompare(playerid)
	local player = self.player_list[playerid]
	player.timeoutGiveupNum = 0	
	local rate = 1
	--比牌花费为当前跟注值 已看牌*2
	if player.look then 
		rate = 2
	end
	local cast = self.followChip*rate
	local canCompare = false
	local lessChip = false
	local reason = self.universal:getReasonId('zjh_bp')
	--如果所剩余筹码小于跟注值让他比牌
	if player.chip < cast then 
		canCompare = true		  
		cast = playerid.chip
		lessChip = true
	elseif self.curRound > 1 then 
		--从第二轮开始才能比牌
		canCompare = true
	end	
	if not canCompare then
		return false
	end
	if cast > 0 then 
		local money = self:callGate(player, 'change_money', -cast, reason)				
		if not money then 
			--扣钱失败
			print("__________compareCard___扣钱失败____",playerid,cast,player.chip)
			return false
		end			
		player.chip = money
		table.insert(self.chipList,tostring(cast))
		-- self.allBetChip = self.allBetChip + cast
		-- player.betChip = player.betChip + cast		
	end
	-- player.bet = cast
	player.oper = self.operList.compare
	local tbSeat = {}
	for k,v in pairs(self.player_list) do 
		if self:isPlaying(v) and playerid ~= k then 
			table.insert(tbSeat,v.seatid)
		end
	end
	if #tbSeat == 1 then
		--只剩下一个人不用选
		tbSeat = {}
	end 
	--跳到比牌状态
	self.fsm:fire("to_compare")
	local data = {zjh_compare_res={
		success=true,
		seat_list = tbSeat,
	}}
	return true,data
end

--比牌选择
function Machine:setPlayerCompare(playerid,seatid,toOpen)
	local player = self.player_list[playerid]
	local player2 = self:getPlayerBySeat(seatid)
	local res1 = self.tbSeatRes[player.seatid]
	local res2 = self.tbSeatRes[seatid]	
	local win = false
	if (player.robot and not player2.robot) or (not player.robot and player2.robot) then 
		self:checkStock()
		res1 = self.tbSeatRes[player.seatid]
	 	res2 = self.tbSeatRes[seatid]			
	end
	print("______________比牌______",player.seatid,seatid)
	if self.rule:compareCard(res1,res2) then
		win = true
		local loser = self:getPlayerBySeat(seatid)
		loser.lose = true		
	else
		player.lose = true		
	end
	self.timer:unregister(self.compareTimehandler)
	self.compareTimehandler = nil
	local data = {zjh_nt_compare={
		from_seat = player.seatid,
		to_seat = seatid,
		win = win,
		all_chip = tostring(self.allBetChip)
	}}	
	self:sendToAll(data)
	if toOpen then 
		self.fsm:fire("to_open")
	end
	return win
end

--弃牌
function Machine:giveup(seatid)
	local player = self:getPlayerBySeat(seatid)
	if not player then 
		print('#########error___player giveup 没有此玩家 seatid',seatid)
		return
	end
	--assert(false)
	print('#########__1111111111_player giveup ',player.playerid,seatid)

	local noGiveup = 0
	local winnerSeat
	for k, v in pairs(self.player_list) do 
		if k ~= player.playerid and self:isPlaying(v) then  
			noGiveup = noGiveup + 1
			winnerSeat = v.seatid
		end
	end
	if noGiveup == 1 then --只剩下一个玩家
		if player.robot then 
			local contrlOk = true
			player.giveup = true
			if not self:checkStock() then 
				player.giveup = false
				if not self:playerAddChip(player.playerid,{oper_type=1,add_chip=0}) then 
					contrlOk = self:playerCompare(player.playerid)
				end
				if contrlOk then 
					--控制成功了 
					return false
				end
			end
		end
		player.giveup = true
		player.bet = 0
		player.oper = self.operList.giveup
		player.state = self.playerState.giveup
		self:ntStateOper(player)
		self.nextOperSeat = false
		self.winnerSeat = { winnerSeat }
		self:setTime()
		self.fsm:fire("oper_to_settlement")
		return true
	elseif noGiveup == 0 then 
		--只剩下你，不能弃牌
		print("____________只剩下你，不能弃牌_____________")
		return false
	end		
	if seatid == self.operSeatid then
		self.nextOperSeat = true
	end	
	player.giveup = true
	player.bet = 0
	player.oper = self.operList.giveup
	player.state = self.playerState.giveup
	self:ntStateOper(player)	
	return true
end


-------------------------------------------------------------------------------
-- 推送消息
-------------------------------------------------------------------------------
function Machine:ntStart()
	print("__________zjh_start")

	local data = {zjh_nt_start={
 		tableid = self.tableid,	
 		paijuid = self.paijuid,
		bottom_num = tostring(self.bottomNum),
		banker_seat = self.startSeatid,
		all_chip = tostring(self.allBetChip),
	}}
	print(data)
	self:sendToAll(data)
end

function Machine:ntOper(seatid,oldOperSeat)
	print("__________zjh_oper",seatid,oldOperSeat)
	local tbOper
	if oldOperSeat then
		local player = self:getPlayerBySeat(oldOperSeat)
		tbOper = {
			seatid = oldOperSeat,
			chip = tostring(player.chip), --所剩筹码
			bet = tostring(player.bet), --这次的下注值
			bet_chip = tostring(player.betChip),--已下了的筹码
			oper = player.oper,
			state = self:getPlayerState(player),
		}
	end
	
	local tbCurOper
	if seatid and self:getPlayerBySeat(seatid) then
		local player = self:getPlayerBySeat(seatid)
		tbCurOper = {
			seatid = seatid, --当前轮到操作的人
	 		oper_time = self.status_time.oper,
	 		state = self:getPlayerState(player),
	 		button_list = self:getPlayerOperList(player),
	 		button_bet = self:getPlayerAddList(player), 		
 		}
	end

	local data = {zjh_nt_oper={
 		tableid = self.tableid,	
 		oper_round = self.curRound,
 		all_chip = tostring(self.allBetChip),
 		pre_oper = tbOper, 		
 		cur_oper = tbCurOper,
	}}
	-- print("_________________data__",data)
	self:sendToAll(data)
end

--推送别人状态的操作
function Machine:ntStateOper(player,oper)
	--print("__________zjh_oper",seatid)
	--local player = self:getPlayerBySeat(seatid)
	if not oper then 
		oper = player.oper
	end
	local tbOper = {
			seatid = player.seatid,
			chip = tostring(player.chip),
			bet = tostring(player.bet),
			bet_chip = tostring(player.betChip),
			oper = oper,
			state = self:getPlayerState(player)
		}	
	local data = {zjh_nt_oper={
 		tableid = self.tableid,	
 		pre_oper = tbOper,
	}}

	self:sendToAll(data)
end

function Machine:ntEnd()
	--print("__________nt_niuniu_end222222222")
	local seatPlayers = {}
	local seatPlayer
	local card
	local cardRes

	local winChip = self.allBetChip 
	--多个人赢
	if self.winnerSeat and #self.winnerSeat > 1 then 
		winChip = winChip / #self.winnerSeat
	end
	local player
	local res,money
	local chip 
	local reason = self.universal:getReasonId('zjh_js')
	for _,seatid in pairs(self.winnerSeat) do 
		player = self:getPlayerBySeat(seatid)
		player.win = true		
		chip = winChip
		if self.roomid ~= 10101 then 
			chip = winChip - player.betChip
			if chip > 0  then --新手场不抽水
				--扣除服务费
				chip = player.betChip + self.universal:setChip(chip*self.incomeRate)
			else
				chip = winChip
			end
		end
		chip = self.universal:setChip(chip)
		money = self:callGate(player,'change_money', chip, reason)		
		-- print("____change_money_____money__",money,winChip, reason)	
		if money then 
			player.chip = money
		end	
		self.startSeatid = seatid	
	end

	for k,v in pairs(self.player_list) do 		
		seatPlayer = self:getSeatPlayer(v)
		if v.playing then 
			--card = self.tbSeatCard[v.seatid]
			cardRes = self.tbSeatRes[v.seatid]
			card = cardRes.card
			seatPlayer.card_data = card
			seatPlayer.card_type = self.rule:getCardType(cardRes.weight)
			for _,seatid in pairs(self.winnerSeat) do 
				if v.seatid == seatid then 
					seatPlayer.state = self.playerState.win
				end
			end
		end
		table.insert(seatPlayers,seatPlayer)
	end

	local data = {zjh_nt_end={
 		tableid = self.tableid,	
 		win_chip = tostring(chip),
 		winer_seat = self.winnerSeat,
 		seat_player = seatPlayers,
	}}
	print("____ntEnd____",data)
	self:sendToAll(data)
	--牌局记录
	for k,v in pairs(seatPlayers) do 
		self:paijuRecord(v)
	end

	--赢得入场分*3 给跑马灯
	for _,seatid in pairs(self.winnerSeat) do 
		if chip > self.minChip * 3 then 
			player = self:getPlayerBySeat(seatid)
		 	broadcast = {
				nt_broadcast = {
					id = 1,
				 	content = '恭喜['..player.nick..']在炸金花游戏中获得金币'..chip,
				 }
			}			
			self.nodeMessage:broadcastAll(broadcast)
		end
	end
end

function Machine:ntRest()
	local data = {zjh_nt_rest={
 		tableid = self.tableid,	
	}}
	self:sendToAll(data)	

end

--推送牌给机器人
function Machine:ntCardToRoot()
	local tbCards = {}
	local seat_players = {}
	local seatPlayer
	local player
	local tbRobot = {}
	for k,v in pairs(self.player_list) do 
		if v.robot then 
			table.insert(tbRobot,v)
		end
	end	
	if not next(tbRobot) then 
		return 
	end
	for seatid,id in pairs(self.seat_list) do
		if id>0 then
			player = self.player_list[id]
			seatPlayer = self:getSeatPlayer(player)
			if self:isPlaying(player) then --正在玩有牌	
				local cardRes = self.tbSeatRes[seatid]
				seatPlayer.card_data  = cardRes.card
			end
			table.insert(seat_players,seatPlayer)
		end
	end	
	local status = {
		tableid = self.tableid,
		seat_player = seat_players,
		all_chip = tostring(self.allBetChip),
		follow_bet = self.followChip,
		oper_round = self.curRound,
	}	
	for _,v in pairs(tbRobot) do 
		print("_______status___",status)
		self:sendMessage(v.playerid,{zjh_status_res = status})
	end
end

----------------------------------
-- 数据操作
----------------------------------
function Machine:calldb(cmd,...)
	return skynet.call('dbmgr_service','lua',cmd,...)	
end

function Machine:senddb(cmd,...)
	skynet.send('dbmgr_service','lua',cmd,...)	
end

--取库存表
function Machine:getStock()
	local stock = self:calldb('get_stock',self.gameid)
	if not next(stock) then 
		stock = {
			gameid=self.gameid,
			pool = 0,
			bottom = 1000,
			top = 100000,
			tip = 0,
		}
		self:calldb('add_stock',stock)		
		stock = self:calldb('get_stock',self.gameid)
	end
	self.stock = stock	
end

function Machine:setStock()
	--机器人库存更新
	local system_earn,tip = self:robotSettlement()		
	if system_earn ~= 0 then 
		self.stock.pool = self.stock.pool + system_earn
		self.stock.tip = self.stock.tip + tip
		self:senddb('update_stock',self.stock)	
	end
end

--计算机器人输赢
function Machine:robotSettlement()
	local systemEarn = 0 --机器人赢的钱
	local tip = 0 --服务费
	local winner = {}
	local maxRes
	local player
	local winChip = self.allBetChip
	for k,v in pairs(self.player_list) do 
		if self:isPlaying(v) and not v.loseEnd then 	
			cardRes = self.tbSeatRes[v.seatid]								
			--找最大牌的玩家			
			if not maxRes then 
				maxRes = cardRes
				winner = {}					
				table.insert(winner,v)						
			else
				local res,same = self.rule:compareCard(maxRes,cardRes)
				if res then							
				elseif same then --一样大
					table.insert(winner,v)							
				else
					maxRes = cardRes
					winner = {}
					table.insert(winner,v)
				end
			end			
		end
	end
	if #winner > 1 then 
		winChip = winChip / #winner
	end
	for k,v in pairs(winner) do 
		if v.robot then 
			systemEarn = systemEarn + winChip
		else
			systemEarn = systemEarn - winChip
		end
	end

	if systemEarn > 0 then 
		systemEarn = self.universal:setChip(systemEarn * (1-self.tipRate))
		tip = self.universal:setChip(systemEarn * self.tipRate)
	elseif systemEarn < 0 then
		systemEarn = self.universal:setChip(systemEarn)
	end
	return systemEarn, tip
end

--检查库存
function Machine:checkStock()
	--1.发牌后预先开牌,机器人和普通人间输赢计算 
	--2.如果库存够赔付正常开牌
	--3.如果库存不够换牌的位置再计算
	self:getStock()
	if not self.stock or not next(self.stock) then --无库存控制
		return false
	end 
	print("____2222222__库存______self.stock_______",self.stock)
	local stock = self.stock
	local function get_earn()	
		local pool = stock.pool
		local bottom = stock.bottom
		local systemEarn = self:robotSettlement()	
		if systemEarn == 0 then --无人在打
			return true
		end
		if pool <= bottom then --库存低于底线时一率杀
			if systemEarn >= 0 then 
				return true
			end
			return false
		end
		--库存 + 机器人输赢  > 0 可开牌
		local earn = pool  + systemEarn
		--print("_____________earn ___",earn,inventory,systemEarn)
		if systemEarn == 0 then --没有机器人
			return true
		elseif earn > 0 then
			--self.stock.pool = earn
			print("__2222222222222______pool___",pool,self.stock)
			return true
		end
		return false
	end

	print("_________________get_earn_____________")
	local res = get_earn()
	if res then
		return true
	end
	print("___________第一道防线____")
	--1.给机器人豹子 
	local seatRes
	local card
	for k,v in pairs(self.player_list) do 
		if self:isPlaying(v) and v.robot then 	
			seatRes = self.tbSeatRes[v.seatid]
			card = self.rule:getBaozi(seatRes)
			self.tbSeatCard[v.seatid] = card
			self.tbSeatRes[v.seatid] = self.rule:getCardWeight(card)			
		end
	end	
	local res = get_earn()
	if res then
		return true
	end	
	log.error('___error_________________库存控制失败了_______',self.tbSeatRes)
	
	return false
end

-------------------------------------------------------------------------------
-- 牌局记录
-------------------------------------------------------------------------------
function Machine:paijuRecord(seatPlayer)
	local playerid 
	local seatid 
	local win 
	local chip
	local beforeStart
	local player 
	local cards
	local cardType
	local endstate
	if not seatPlayer.playing then
		return 
	end
	playerid = seatPlayer.playerid		
	seatid = seatPlayer.seatid
	cards = seatPlayer.card_data
	cardType = seatPlayer.card_type
	win = 0
	player = self.player_list[playerid]
	if player.isRecorded then 
		return 
	end
	player.isRecorded = true 
	chip = player.chip
	beforeStart = self.tbBeforeStart[playerid]
	-- print("________222___",playerid,self.tbBeforeStart)
	if player.win then 
		win = 1
	end
	endstate = self:getEndState(player) --结束时状态
    --牌局记录写到redis 
	local record = {
		_type = "playercard" .. self.gameid,
		mid = playerid,
		appid = player.appid,
		channelid = player.channelid,			
		gameid = self.gameid,
		levelid = self.roomid,
		tableid = self.tableid,
		cardid = self.paijuid,
		before = beforeStart,
		after = chip,
		iswin = win,
		optnum = chip - beforeStart,
		updatetime = os.time(),			
		cards = cards,
		cardtype = self.rule:getCardTypeById(cardType),
		state = endstate, --玩家以什么状态结束的
	}
	send_redis({ "rpush", "logs:queue_list", json.encode(record) }, 1)


end






-----------------------------------------------------------------------------------
----------------------------------
-- 数据操作
----------------------------------
--当前操作所剩余的时间
function Machine:getOperTime()
	local time = self.status_time[self.fsm:get()] - (os.time() - self.operStartTime)
	if time < 0 then 
		time = 0
	end
	return time
end

--当前状态
function Machine:getState()
	return self.fsm:get()
end

--状态对应的数字
function Machine:getStateNum()
	local state = self:getState()
	for k, v in pairs(self.gameStateTransitionTable) do 
		if state == v[1] then 
			return k
		end
	end
	return 
end

--桌子相关配置
function Machine:getConfig()
	return {
		bottom_num	= tostring(self.bottomNum),	-- 底分		
		add_list = self.addChipList[self.bottomNum],
		max_round   = self.maxRound, 	--最大轮数
		time_rest = self.status_time.rest,
		time_start = self.status_time.start,
		time_compare = self.status_time.compare,
		time_end = self.status_time.settlement,		
		time_oper = self.status_time.oper,
	}
end

--取牌
function Machine:getCard(playerid,num)
	local tbCard = {}
	local playerCard = self.gmcards[playerid] 
	local delCard = {}
	if playerCard and #playerCard>0 and self.tbCheatNum[playerid] > 0 then --有配牌		
		self.tbCheatNum[playerid] = self.tbCheatNum[playerid] - 1
		if self.tbCheatNum[playerid] == 0 then
			self.gmcards[playerid] = {}
		end	
		if num == 1 then 
			table.insert(tbCard,playerCard[5])
		else
			for i=1,num do 
				table.insert(tbCard,playerCard[i])
			end
		end

		return tbCard
	end

	return self.rule:getCard(num)
end

function Machine:setGmCard(playerid,cards,repeatNum)

	self.tbCheatNum[playerid] = repeatNum or 0
	if repeatNum  == 0 then
		self.gmcards[playerid] = {}
	else
		self.gmcards[playerid] = cards
	end
	print("_______cards#################___________________",cards)
end

--进入桌子
function Machine:enterTable(playerid,player_info,open_card)
	print('玩家进入牌桌>>>>>>>>>>>',player_id,self.tableid,open_card)
	
end

--离开桌子
function Machine:leaveTable(playerid)
	print('玩家离开牌桌>>>>>>>>>>>',playerid,self.tableid)
	self:sitUp(playerid)
end

--掉线处理
function Machine:disconnect(playerid)
	print('玩家掉线>>>>>>>>>>>',playerid,self.tableid)
	local player = self.player_list[playerid]
	--旁观者掉线马上踢出大厅
	if not player.playing and player.disconnect then 
		print("########在旁观玩家中直接踢走")	
		-- skynet.fork(function() --防止出调用回路
		-- 	skynet.sleep(50) 
			self:sendGate(player, 'update_tableid', 0)
			self:sitUp(playerid)

		-- end)
		return true
	end	
	return false
end

function Machine:exit()
	self.nodeMessage:sendService('.proxy','clear_table',self.tableid)
	skynet.fork(function()
		skynet.sleep(300)
		skynet.exit()
	end)
end

function Machine:setClose(close)
	print("_____33333____Machine____setClose___",close,self.player_list)
	--定时踢出所有玩家后，关服
	self.close = close
	if not self.closeHandler then 
		self.timer:unregister(self.closeHandler)
		self.closeHandler = nil
	end
	if close then 
		local maxDelay = 5*60 --最大的等待时间
		local delay = 0
		local callback = function()
			delay = delay + 1
			if delay > maxDelay then
				self.fsm:fire('to_rest') 
				self.fsm:set('rest')
				self:onRest()
			end
			if self:getState() ~= "rest" and self:getState() ~= "wait" then 
				return
			end
			for k,v in pairs(self.player_list) do 
				if close then 				
					local data = {nt_kick_table = {
						playerid = k,
						content = '游戏正在维护！',
					}}
					self:sendMessage(k,data)	
					--设置玩家所在桌子id为0	
					self:sendGate(v, 'update_tableid', 0)
					self:sitUp(k)
					self.player_list[k] = nil
				end
			end		
			self.timer:unregister(self.closeHandler)			
			self:exit()
		end
		--1局玩完后关游戏
		self.closeHandler = self.timer:register(1 , nil, callback,true)
	end	
end

----------------------------------------------------------------------------------------------
---------开始游戏
---------------------------------------------------------------------------------------------- 
function Machine:start()
	self.fsm:set("rest")
	self.fsm:fire("to_wait")
end

return Machine